package com.dengwei.smali.first.dwpull_to_refresh.PullToRefresh.indicator;

import android.graphics.PointF;

/**
 * Created by dengwei on 2017/3/18.
 */

public class PullToRereshIndicator {

    public final static int POS_START = 0;                  //开始位置
    private int mCurrentPos = 0;                            //当前位置
    private int mLastPos = 0;                               //结束前的位置
    private float mResistance = 1.7f;                       //阻尼系数
    private float mRatioOfHeaderHeightToRefresh = 1.2f;
    private boolean mIsUnderTouch;
    private int mPressedPos;
    private PointF mLastMove = new PointF();
    private float mOffsetX;
    private float mOffsetY;
    private int mOffsetToKeepHeaderWhileLoading = -1;
    private int mOffsetToRefresh;
    private int mHeaderHeight;                              //头部高度
    private int mRefreshCompleteY = 0;


    public void onRelease() {
        mIsUnderTouch = false;
    }

    public float getResistance(){
        return mResistance;
    }

    public void setResistance(float resistance){
        this.mResistance = resistance;
    }

    public float getRatioOfHeaderHeightToRefresh(){
        return mRatioOfHeaderHeightToRefresh;
    }

    public void setRatioOfHeaderHeightToRefresh(float ratioOfHeaderHeightToRefresh) {
        this.mRatioOfHeaderHeightToRefresh = ratioOfHeaderHeightToRefresh;
        mOffsetToRefresh = (int) (mHeaderHeight * ratioOfHeaderHeightToRefresh);
    }

    public final void setCurrentPos(int currentPos){
        mLastPos = mCurrentPos;
        mCurrentPos = currentPos;
    }

    public int getCurrentPos(){
        return mCurrentPos;
    }

    public float getOffsetX() {
        return mOffsetX;
    }

    public float getOffsetY() {
        return mOffsetY;
    }

    public int getLastPosY() {
        return mLastPos;
    }

    public int getHeaderHeight(){
        return mHeaderHeight;
    }

    public void setHeaderHeight(int headerHeight){
        this.mHeaderHeight = headerHeight;
        updateHeight();
    }

    public void onPressDown(float x, float y){
        mIsUnderTouch = true;
        mPressedPos = mCurrentPos;
        mLastMove.set(x, y);
    }

    public int getOffsetToKeepHeaderWhileLoading() {
        return mOffsetToKeepHeaderWhileLoading >= 0 ? mOffsetToKeepHeaderWhileLoading : mHeaderHeight;
    }

    public int getOffsetToRefresh(){
        return mOffsetToRefresh;
    }

    public final void onMove(float x, float y){
        float offsetX = x - mLastMove.x;
        float offsetY = y - mLastMove.y;
        setOffset(offsetX, offsetY / mResistance);
        mLastMove.set(x, y);
    }

    public boolean goDownCrossFinishPosition() {
        return mCurrentPos >= mRefreshCompleteY;
    }

    protected void setOffset(float x, float y){
        mOffsetX = x;
        mOffsetY = y;
    }

    public boolean hasLeftStartPosition(){
        return mCurrentPos > POS_START;
    }

    public boolean isOverOffsetToKeepHeaderWhileLoading(){
        return mCurrentPos > getOffsetToKeepHeaderWhileLoading();
    }

    public boolean isOverOffsetToRefresh(){
        return mCurrentPos >= getOffsetToRefresh();
    }

    public boolean isUnderTouch(){
        return mIsUnderTouch;
    }

    public void onUIRefreshComplete(){
        mRefreshCompleteY = mCurrentPos;
    }

    /**
     * 是否小于初始位置
     * @param to
     * @return
     */
    public boolean willOverTop(int to) {
        return to < POS_START;
    }

    public boolean isAlreadyHere(int to) {
        return mCurrentPos == to;
    }

    public float getLastPercent() {
        final float oldPercent = mHeaderHeight == 0 ? 0 : mLastPos * 1f / mHeaderHeight;
        return oldPercent;
    }

    public float getCurrentPercent() {
        final float currentPercent = mHeaderHeight == 0 ? 0 : mCurrentPos * 1f / mHeaderHeight;
        return currentPercent;
    }

    /**
     * 是否是开始位置
     * @return
     */
    public boolean isInStartPosition(){
        return mCurrentPos == POS_START;
    }

    /**
     * 是否是Down事件的位置
     * @return
     */
    public boolean hasMoveAfterPressdDown(){
        return mCurrentPos != mPressedPos;
    }

    public boolean hasJustLeftStartPosition(){
        return mLastPos == POS_START && hasLeftStartPosition();
    }

    public boolean hasJustBackToStartPosition(){
        return mLastPos != POS_START && isInStartPosition();
    }

    protected void updateHeight() {
        mOffsetToRefresh = (int) (mRatioOfHeaderHeightToRefresh * mHeaderHeight);
    }

    public boolean crossRefreshLineFromTopToBottom() {
        return mLastPos < getOffsetToRefresh() && mCurrentPos >= getOffsetToRefresh();
    }

    public boolean hasJustReachedHeaderHeightFromTopToBottom(){
        return mLastPos < mHeaderHeight && mCurrentPos >= mHeaderHeight;
    }

    public void convertFrom(PullToRereshIndicator indicator){
        mCurrentPos = indicator.mCurrentPos;
        mLastPos = indicator.mLastPos;
        mHeaderHeight = indicator.mHeaderHeight;
    }

}
